/**
 * 通用型组件的组装 增删改查
 */
import moment from 'moment'
export const Slime = {
  data() {
    return {
      /* 查询条件-请不要在queryParam中声明非字符串值的属性 */
      queryParam: {},
      queryParamRangePicker:null, //时间区间
      /* table加载状态 */
      loading: false,
      /* 弹窗加载状态 */
      confirmLoading: false,
      /* 表单显示 */
      visible: false,
      /* 表单属性 */
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条';
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0,
        size:'default',
      },
      /* table选中keys*/
      selectedRowKeys: []
    };
  },
  created() {},
  methods: {
    //封装初始话list
    loadDataPack(arg){ // arg=1 会进行页码初始化
      let that = this;
      return new Promise(function(resolve, reject){
        if (arg === 1) {  
          that.ipagination.current = 1;
        }
        that.loading = true;
        let params = that.getQueryParams(); // 查询条件
        CRUD.getList(params)
          .then((res) => {
            resolve(res);
            // 页码
            that.ipagination.total = res.result.total;
          })
          .catch((res) => {
            reject(res);
          })
          .finally(() => {
            that.loading = false;
          });
      })
    },
    // 增-弹窗
    crudCreate(data) {
      this.$refs.updateForm.crudCreate(data);
    },
    // 改-弹窗
    crudUpdate(data) {
      this.$refs.updateForm.crudUpdate(data);
    },
    // 删
    crudDelete(id) {
      const that = this;
      let param = [];
      param.push(id);
      CRUD.del(param)
        .then((res) => {
          that.$message.success('删除成功');
        })
        .finally(() => {
          that.loadData();
        });
    },
    // 查
    searchQuery() {
      var param = this.queryParam;
      this.$router.push({ query: param })
      this.loadData(1);
    },
    // 查 重置
    searchReset() {
      this.queryParam = {};
      this.queryParamRangePicker = null;
      this.$router.push({ query: {} })
      this.loadData(1);
    },
    // 弹窗-关闭
    closeModal() {
      this.loadData();
    },
    // 分页
    handleTableChange(pagination, filters, sorter) {
      var param = {...this.queryParam, pageNo:pagination.current};
      this.$router.push({ query: param })
      //分页、排序、筛选变化时触发
      this.ipagination = pagination;
      this.loadData();
    },
    // 查询条件
    getQueryParams(){
      let query = this.$route.query
      let keys = Object.keys(query);
      let qu = {}
      keys.map((obj)=>{
        let intVal = isNonnum(query[obj])
        if(intVal){ //数字
          qu[obj] = parseInt(query[obj]) 
        }else if(obj == 'startCreateDate' || obj == 'endCreateDate'){  //时间
          qu[obj] = query[obj]
          this.queryParamRangePicker = [moment(query['startCreateDate'], 'YYYY-MM-DD'), moment(query['endCreateDate'], 'YYYY-MM-DD')]
        }else{  //其他
          qu[obj] = query[obj]
        }
      })

      this.queryParam = {
        ...qu,
        ...this.queryParam,
      };
      var param = this.queryParam
      this.ipagination.current = qu.pageNo ? parseInt(qu.pageNo) : this.ipagination.current
      param.pageNo = this.ipagination.current;
      param.pageSize = this.ipagination.pageSize;
      return param

      function isNonnum(str){
        let myreg = /^[0-9]*$/;
        if(myreg.test(str)){
          return true;
        }else{
          return false;
        };
      };
    },
    // 多选
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectionRows = selectionRows;
    },
    // 权限配置
    permissionRole(permission){  //存在 true 不存在 false //使用方式 :disabled="!permissionRole('contact::del')"     
      let roles = this.$store.getters.roles
      let rolesVal = roles.indexOf(permission)

      if(roles[0] == 'admin'){
        return true
      }else if(roles[0] == 'ROLE_SYSTEM_DEFAULT'){
        return false 
      }else if(rolesVal == -1){
        return false
      }else{
        return true
      }
    },

    // 金额分转元
    funMoneyFixed(money){
      if (parseInt(money) == 0) {
        return '0.00'
      } else if (parseInt(money) > 0) {
        return (parseInt(money) / 100).toFixed(2)
      } else {
        return '';
      }
    },
    //金额的浮点算法
    numberRound(val){
      return Math.round((val * 10000) / 100)
    },
    //ant 图片转数组
    antImageArr(data){
      return data ? data.split(",") : []
    },
    //表格导出--转化为json
    formatJson(filterVal, jsonData) {
      return jsonData.map((v) => filterVal.map((j) => v[j]))
    },
  },
  filters: {
    //金钱分转元
    money: function (money) {
      if (money == 0) {
        return '0.00'
      } else if (money > 0) {
        return (money / 100).toFixed(2)
      } else {
        return ''
      }
    },
    //金钱分转元
    filtersMoney: function (money) {
      if (parseInt(money) == 0) {
        return '0.00'
      } else if (parseInt(money) > 0 || parseInt(money) < 0) {
        return (parseInt(money) / 100).toFixed(2)
      } else {
        return '';
      }
    },
    //支付方式
    payChannel: function (payChannel) {
      switch (payChannel) {
        case 'ali_pay' : return "支付宝";
        default: return '';
      }
    },
    //订单状态
    orderStatus(status){
      switch (status) {
        case 0:
          return '待付款'
        case 1:
          return '已取消'
        case 2:
          return '待配送'
        case 3:
          return '出库失败,待退款'
        case 4:
          return '已退款'
        case 5:
          return '已配送'
        default: return ''; 
      }
    },
    mobileHide(mobile){
      return mobile.slice(0,3) + "****" + mobile.slice(7,11)
    }
  }
};
let CRUD = {}
export function crud(options) {
  CRUD = options
  return options;
}
